package com.qidian.md.service.impl;

import com.qidian.md.mapper.UnitmeasureMapper;
import com.qidian.md.service.UnitmeasureService;
import com.qidian.mes.pojo.md.Unitmeasure;
import com.qidian.mes.util.pojo.MESResult;
import com.qidian.mes.util.pojo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 18林梦雅
 * @Company 起点编程
 * @Time 2023/10/30 16:49
 * @Description
 */
@Service
public class UnitmeasureServiceImpl implements UnitmeasureService {

    @Autowired
    private UnitmeasureMapper unitmeasureMapper;


    @Override
    public MESResult queryByPage(Unitmeasure unitmeasure, int page, int limit) {
        Pager<Unitmeasure> pager=new Pager<>();
        pager.setCurr(page);
        pager.setLimit(limit);
        pager.setTotal( unitmeasureMapper.selectCount(unitmeasure));
        pager.setData(unitmeasureMapper.selectByPage(unitmeasure,(page-1)*limit,limit));
        pager.setMaxPageNum((int) Math.ceil(pager.getTotal()*1.0/limit));
        return MESResult.ok(pager);

    }



    /**
     * 添加单位,先判断单位名是否重复,再添加
     * @param unitmeasure
     * @return
     */
    @Override
    public MESResult addUnitmeasure(Unitmeasure unitmeasure) {
//
        return MESResult.updateIsOk(unitmeasureMapper.addUnitmeasure(unitmeasure));
    }



    /**
     * 先根据Id查询内容,返回给前端的编辑页面
     * @param measureId
     * @return
     */
    @Override
    public MESResult queryById(Long measureId) {
        Unitmeasure unitmeasure = null;
        if ((unitmeasure = unitmeasureMapper.selectById(measureId)) != null){
            return MESResult.ok(unitmeasure);
        }
        return MESResult.empty();
    }
    /**
     * 根据客户id-->客户修改编辑
     * 直接更新数据,不用判断客户名是否重复,编码是不一样的
     * @param unitmeasure
     * @return
     */
    @Override
    public MESResult update(Unitmeasure unitmeasure) {

        return MESResult.updateIsOk(unitmeasureMapper.update(unitmeasure));

    }



    /**
     * 单行删除功能
     * @param measureId
     * @return
     */
    @Override
    public MESResult deleteUpdateStatus(Long measureId) {
        return MESResult.ok(unitmeasureMapper.deleteUpdateStatus(measureId));
    }







}
